package l3s.wikirevisions;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.solr.client.solrj.beans.Field;

public class Event implements Serializable {
	@Field
	private String ID;
	
	@Field
	private Date start;
	
	@Field
	private Date end;
		
	@Field
	private double score;
	
	@Field
	private String text;
	
	@Field
	private String description;
	
	@Field
	private List<String> entity;
	
	private int index;
	
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	private List<String> snippets = new ArrayList<String>();
	
	
	public List<String> getSnippets() {
		return snippets;
	}	

	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public void setSnippets(List<String> snippets) {
		this.snippets = snippets;
	}
	
	public String getSnippetsHtml(){
		String str = "";
		if (snippets == null)
			return str;
		for (String s: snippets)
			str += s + "...";
		return str;
	}

	public String getText() {
		return text;
	}
	
	public List<String> getEntity() {
		return entity;
	}

	public void setEntities(List<String> entities) {
		this.entity = entities;
	}


	public void setText(String text) {
		this.text = text;
	}
	
	public String getEntitiesStr(){
		String s = "";
		for (String e:entity)
			s += e + ", ";
		if (s.length() > 0)
			s = s.substring(0, s.length() - 2);
		
		return s;
	}


	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}


	public Date getStart() {
		if (start != null)
			return start;
		return start;
	}
	
	public String getStartStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(getStart());
	}

	public void setStart(Date start) {
		this.start = start;
	}
	
	public Date getEnd() {
		if (end != null)
			return end;
		return end;
	}
	
	public String getEndStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(getEnd());
	}

	public void setEnd(Date end) {
		this.end = end;
	}	

	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	@Override
	public String toString() {
		return "***ID: " + ID + " ***Text: " + getText();
	}
	
	public static void main(String args[]){
		String htmlText = "http://localhost:8080/File:Ann_Dunham_with_father_and_children.jpg";
		htmlText = htmlText.replaceFirst("localhost:\\d*", "wikipedia.org/wiki");
		System.out.println(htmlText);
	}
	
	public String getLink(){
		return "Wiki.action?";
	}
	
	public String getJsonForTimeline(){
		StringBuilder jsonForTimeline = new StringBuilder();
         jsonForTimeline.append("{\"startDate\":\"");
         Calendar cal = Calendar.getInstance();
         cal.setTime(start);
         int year = cal.get(Calendar.YEAR);
         int month = cal.get(Calendar.MONTH)+1;
         int day = cal.get(Calendar.DAY_OF_MONTH);
         jsonForTimeline.append(year);
         jsonForTimeline.append(",");
         jsonForTimeline.append(month);
         jsonForTimeline.append(",");
         jsonForTimeline.append(day);
          jsonForTimeline.append("\",");
          
          cal.setTime(end);
          year = cal.get(Calendar.YEAR);
          month = cal.get(Calendar.MONTH)+1;
          day = cal.get(Calendar.DAY_OF_MONTH);
         jsonForTimeline.append("\"endDate\":\"");
         jsonForTimeline.append(year);
         jsonForTimeline.append(",");
         jsonForTimeline.append(month);
         jsonForTimeline.append(",");
         jsonForTimeline.append(day);
          jsonForTimeline.append("\",");
         jsonForTimeline.append("\"headline\":\"");   
         jsonForTimeline.append(description.replaceAll("\\s+", " "));
         jsonForTimeline.append("\",");
         jsonForTimeline.append("\"text\":\"");  
         jsonForTimeline.append("<p>");
         jsonForTimeline.append(description.replaceAll("\\s+", " "));
         //jsonForTimeline.append(getLink().replaceAll("\\s+", " "));
         jsonForTimeline.append("</p>");
         if (entity != null){
	         jsonForTimeline.append("<p>");
	         jsonForTimeline.append(entity.toString());
	         jsonForTimeline.append("</p>");
         }
         
         jsonForTimeline.append("\",");
         jsonForTimeline.append("\"classname\":\"optionaluniqueclassnamecanbeaddedhere\",\"asset\":{}}");
         
         return jsonForTimeline.toString();
	}
}
